package com.bblocks.common.anti_reptile.rule;

import com.bblocks.common.anti_reptile.config.bean.RuleComm;
import com.bblocks.common.anti_reptile.constant.RuleResultEnum;
import com.bblocks.common.anti_reptile.rateLimiter.RateLimiterPlan;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jin
 * @since 2024/3/23 14:40
 */
public interface AntiReptileRule {
    /**
     * 反爬规则具体实现
     * @param request 请求
     * @param response 响应
     * @return true为击中反爬规则
     */
    RuleComm match(HttpServletRequest request, HttpServletResponse response);

    /**
     * 重置已记录规则
     * @param request 请求
     * @param realRequestUri 原始请求uri
     */
    void reset(HttpServletRequest request, String realRequestUri);

    /**
     * 规则优先级
     * @return 优先级
     */
    int getOrder();


    /**
     * 重置-验证码解除，未清理限流数据
     * @param rule
     * @param rateLimiterPlan
     * @param id
     * @return 1=成功
     */
    default int resetDefault(RuleComm rule, RateLimiterPlan rateLimiterPlan, String id) {
        RuleResultEnum resultEnum = RuleResultEnum.getRuleResult(rule.getLockType());
        if(resultEnum == RuleResultEnum.RATELIMIT_CAPTCHA){
            int ret = rateLimiterPlan.outBlack(rule,id);
            return ret;
        }
        return 0;
    }
}
